import {pageInit} from "../../utils/interceptor";
import {getLoginUser, logout} from "../../service/user";
import {getSite} from "../../service/site";
import storeManagerService from "../../service/storeManager";
import {simpleGet} from "../../utils/request";


const app = getApp();

Page(pageInit({
    data: {
        nav:{
            title:"我的",
            background:"#fff",
            opacity:0,
        },
        imageUrl: app.globalData.imageUrl,
        user:null,
        site:null,
        storeManager:null,
        options:{},
        saleGrid:[],
        center:null,
    },
    onLoad: function (options = {}) {


        (async () => {
            //隐藏转发分享
            wx.hideShareMenu({
                menus: ['shareAppMessage', 'shareTimeline']
            })
        })()

    },
    onShow(){
        (async () => {
            const user = await getLoginUser(true)
            const site = await getSite()
            const storeManager = await storeManagerService.findByUid(user.id)
            const {center} = await simpleGet('/user/center')
            this.setData({
                user,
                site,
                storeManager,
                center,
            })
        })()
    },
    /**
     * 获取微信登录
     */
    getLogin: function (e) {
        let {user} = this.data
        login().then(res => {
            // 登录成功，刷新页面
            this.onShow()
        })
    },
    /**
     * 获取用户手机号
     */
    getPhoneNumber: function (e) {
        let code = e.detail.code
        let {shop} = this.data
        if (code){
            request({
                url:'/public/get_mobile',
                data:{
                    code:code,
                    shop_id:shop.id,
                }
            }).then(mobile => {
                login({
                    mobile:mobile
                }).then(({isLogin, userInfo}) => {

                    // 登录成功，刷新页面
                    this.onShow()
                })
            })
        }
    },

    doLogout(){
        logout()
        wx.reLaunch({
            url:'/pages/login/index'
        })
    },
}))
